﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using Facebook;
using System.ComponentModel;
using System.Windows;
using System.Collections.ObjectModel;
using WpfApplication1.ViewModel;

namespace WpfApplication1.Utils
{
    class AutoUpdate
    {
        private CFacebookServer fbServer = new CFacebookServer(MainWindow.AccessToken);
        private RootData rootdata = new RootData();
        private AnnouncementSystem announ = new AnnouncementSystem();

        private static double _timeHomeFeed = 0;
        private static double _timeNotify = 0;
        private static double _timeFriend = 0;
        private static double _timeWall = 0;
        

        private DispatcherTimer _timerHomeFeed;
        private DispatcherTimer _timerNotify;
        private DispatcherTimer _timerFriend;
        private DispatcherTimer _timerWall;

        public double TimeHomeFeed
        {
            get { return _timeHomeFeed; }
        }

        

        public AutoUpdate()
        {
        }

        public void StartAutoUpdateHomeFeed()
        {
            try
            {
                _timerHomeFeed = new DispatcherTimer();
                _timerHomeFeed.Tick += new EventHandler((object sender, EventArgs e) =>
                {
                    Thread thread = new Thread(new ThreadStart(CheckAndUpdateHomeFeed));
                    thread.SetApartmentState(ApartmentState.STA);
                    thread.Start();
                });
                _timerHomeFeed.Interval = new TimeSpan(0, 1, 0);
                _timerHomeFeed.Start();
            }
            catch
            {
                return;
            }
                    
        }


        void CheckAndUpdateHomeFeed()
        {
            try
            {
                if (_timeHomeFeed != 0)
                {
                    IDictionary<string, object> data = fbServer.GetHomeFeed(_timeHomeFeed);
                    ObservableCollection<HomeFeedViewModel> list = fbServer.ConvertToListFeeds(data);
                    DoAddAnnouncement(list.Count.ToString() + " home feed(s) updated... la thang nao hu hu chan qua");
                    //DoAddAnnouncement(list.Count.ToString() + " home feed(s) updated...");
                    DoAddHomeFeedFirst(list);
                }
            }
            catch 
            {
                return;
            }
            
        }

        public void StartAutoupdateNotification()
        {
            try
            {
                _timerNotify = new DispatcherTimer();
                _timerNotify.Tick += new EventHandler((object sender, EventArgs e) =>
                {
                    Thread thread = new Thread(new ThreadStart(CheckAndUpdateNotify));
                    thread.Start();
                });
                _timerNotify.Interval = new TimeSpan(0, 1, 0);
                _timerNotify.Start();
            }
            catch 
            {
                return;
            }
            
        }

        void CheckAndUpdateNotify()
        {
            try
            {
                if (_timeNotify != 0)
                {
                    ObservableCollection<NotificationViewModel> list = fbServer.GetListNotifications(_timeNotify);
                    DoAddAnnouncement(list.Count.ToString() + " Notify updated...");
                    for (int i = 0; i < list.Count; i++)
                        DoAddAnnouncement(list[i].Title);
                    DoAddNotificationFirst(list);
                }
            }
            catch 
            {
                return;
            }
            
        }

        public void StartAutoupdateFriendRequest()
        {
            try
            {
                _timerFriend = new DispatcherTimer();
                _timerFriend.Tick += new EventHandler((sender, e) =>
                {
                    Thread thread = new Thread(new ThreadStart(CheckAndUpdateFriendRequest));
                    thread.Start();
                });
                _timerFriend.Interval = new TimeSpan(0, 1, 0);
                _timerFriend.Start();
            }

            catch 
            {
                return;
            }
            
        }

        void CheckAndUpdateFriendRequest()
        {
            try
            {
                if (_timeFriend != 0)
                {
                    ObservableCollection<FriendRequestViewModel> list = fbServer.GetListFriendRequest(_timeNotify);
                    DoAddAnnouncement("FriendRequest Updated...");
                    for (int i = 0; i < list.Count; i++)
                        DoAddAnnouncement(list[i].Name + " want to be your friend...");
                    DoAddFriendRequest(list);
                }
            }
            catch 
            {
                return;
            }
            
        }

        public void StartAutoUpdateWall()
        {
            _timerWall = new DispatcherTimer();
            _timerWall.Tick += new EventHandler((sender, ex) =>
            {
                Thread thread = new Thread(new ThreadStart(CheckAndUpdateWall));
                thread.Start();
            });
            _timerWall.Interval = new TimeSpan(0, 1, 0);
            _timerWall.Start();
        }

        void CheckAndUpdateWall()
        {
            if (_timeWall != 0)
            {
                DoAddAnnouncement("Your wall updated");
                DoAddWall();
            }
        }

        public void UpdateTimeHomeFeed()
        {
            _timeHomeFeed = GetUnixTimeNow();
        }
        public void UpdateTimeNotify()
        {
            _timeNotify = GetUnixTimeNow();
        }
        public void UpdateTimeFriendRequest()
        {
            _timeFriend = GetUnixTimeNow();
        }
        public void UpdateTimeWall()
        {
            _timeWall = GetUnixTimeNow();
        }

        public double GetUnixTimeNow()
        {
            return ConvertToTimestamp(DateTime.Now);
        }

        private double ConvertToTimestamp(DateTime value)
        {
            //create Timespan by subtracting the value provided from
            //the Unix Epoch
            TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

            //return the total seconds (which is a UNIX timestamp)
            return (long)span.TotalSeconds;
        }

        #region Delegate

        public delegate void AddFeedFirstDelegate(ObservableCollection<HomeFeedViewModel> list);
        public delegate void AddNotificationFirstDelegate(ObservableCollection<NotificationViewModel> list);
        public delegate void AddFriendRequestDelegate(ObservableCollection<FriendRequestViewModel> list);
        public delegate void AddWallDelegate();
        public delegate void AddAnnoncement(string message);

        public AddFeedFirstDelegate DoAddHomeFeedFirst;
        public AddNotificationFirstDelegate DoAddNotificationFirst;
        public AddFriendRequestDelegate DoAddFriendRequest;
        public AddWallDelegate DoAddWall;
        public AddAnnoncement DoAddAnnouncement;


        #endregion
    }
}
